package StartUp;

import Csv2RDF.CSVReader;
import Csv2RDF.Relation2RDF;
import Csv2RDF.RelationExtraction;
import Json2RDF.Json2Java.Json2Java;
import Json2RDF.Surface2RDF.IndexTable.*;
import Json2RDF.Surface2RDF.SurfaceWriter.*;
import Json2RDF.Surface2RDF.FaceManager.PartFaceManager;
import Json2RDF.Surface2RDF.FaceManager.PlaneEdgeManager;
import RDFManager.RDFOutput;
import RDFManager.RDFReader;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
  public static void main(String[] args) {
    long startTime = System.currentTimeMillis();

    RDFReader.model = RDFReader.createModel(args[0]);

    Json2Java.JsonExtraction(args[1]);

    ExecutorService executorService = Executors.newFixedThreadPool(9);
    executorService.submit(Main::method1);
    executorService.submit(Main::method2);
    executorService.submit(Main::method3);
    executorService.submit(Main::method4);
    executorService.submit(Main::method5);
    executorService.submit(Main::method6);
    executorService.submit(Main::method7);
    executorService.submit(Main::method8);
    executorService.submit(Main::method9);

    executorService.shutdown();
    while (!executorService.isTerminated()) {
    }

    System.out.println("All tasks completed");

    Map<Resource, List<Resource>> partFaces = PartFaceManager.FacesToParts(PartId2ResourceIndexTable.partResourceIdxTbl, Face2ResourceIndexTable.faceResourceIdxTbl);
    if (partFaces != null) {
      PartFaceWriter.write(partFaces);
    }

    Map<Resource, List<Resource>> planeEdges = PlaneEdgeManager.EdgesToPlanes(Plane2ResourceIndexTable.planeResourceIdxTbl, EdgeId2ResourceIndexTable.edgeResourceIdxTbl);
    if (planeEdges != null) {
      PlaneEdgeWriter.write(planeEdges);
    }


    RelationExtraction.relationExtraction(CSVReader.ReadCSVFile(args[2]), FaceIndexTable.faceIdxTbl);
    FaceIndexTable.faceIdxTbl.forEach((k, v) -> {
      Relation2RDF.writeRelation(v);
    });

    RDFOutput.out_turtle(RDFReader.model, args[3]);
//    RDFOutput.out_rdf(RDFReader.model, "experiment_face_rdf.rdf");
    RDFOutput.out_nTriples(RDFReader.model, args[4]);

    long endTime = System.currentTimeMillis();
    System.out.println("code run cost : " + (endTime - startTime) + " ms");
  }


  private static synchronized void method1() {
    if (Json2Java.cylindrical_surface_list != null) {
      CylindricalSurfaceWriter.writeList(Json2Java.cylindrical_surface_list);
    }
  }

  private static synchronized void method2() {
    if (Json2Java.conical_surface_list != null) {
      ConicalSurfaceWriter.writeList(Json2Java.conical_surface_list);
    }
  }

  private static synchronized void method3() {
    if (Json2Java.spherical_surface_list != null) {
      SphericalSurfaceWriter.writeList(Json2Java.spherical_surface_list);
    }
  }

  private static synchronized void method4() {
    if (Json2Java.toroidal_surface_list != null) {
      ToroidalSurfaceWriter.writeList(Json2Java.toroidal_surface_list);
    }
  }

  private static synchronized void method5() {
    if (Json2Java.toroidal_plane_list != null) {
      ToroidalPlaneWriter.writeList(Json2Java.toroidal_plane_list);
    }
  }

  private static synchronized void method6() {
    if (Json2Java.edge_list != null) {
      EdgeWriter.writeList(Json2Java.edge_list);
    }
  }

  private static synchronized void method7() {
    if (Json2Java.polygonal_plane_list != null) {
      PolygonalPlaneWriter.writeList(Json2Java.polygonal_plane_list);
    }
  }

  private static synchronized void method8() {
    if (Json2Java.part_list != null) {
      PartWriter.writeList(Json2Java.part_list);
    }
  }
  private static synchronized void method9() {
    if (Json2Java.b_conicalsurface_list != null) {
      b_conicalSurfaceWriter.writeList(Json2Java.b_conicalsurface_list);
    }
  }

}
